"""
数据库表模型定义
"""
from typing import Type, Optional, List
from pydantic import Field

from tortoise import fields, Model
from tortoise.contrib.pydantic import pydantic_model_creator

from core.model import AbstractBaseModel, TimestampMixin


class Role(TimestampMixin, AbstractBaseModel):
    name = fields.CharField(max_length=32, unique=True, description='角色名')
    value = fields.CharField(max_length=32, unique=True, description='角色值', index=True)
    remark = fields.CharField(max_length=256, description='角色描述')
    orderNo = fields.IntField(description='排序', default=0)
    status = fields.BooleanField(default=False, description='状态')
    menu = fields.JSONField(max_length=256, description='菜单，例：[1,2,3]', null=True, default=None)
    permission = fields.JSONField(null=True, default=None, description='权限，例：[1,2,3]')

    class Meta:
        table = 'role'
        table_description = '角色表'
        ordering = ["-orderNo", "-created_at", "id"]

    class PydanticMeta:
        exclude = ["created_at", "modified_at", "is_delete"]


class Dept(TimestampMixin, AbstractBaseModel):
    name = fields.CharField(max_length=32, unique=True, description='部门名称')
    parent = fields.ForeignKeyField('models.Dept', description='上级部门', null=True, related_name='children')
    orderNo = fields.IntField(description='排序', default=0)
    remark = fields.CharField(max_length=256, description='部门描述', null=True, default=None)
    status = fields.BooleanField(default=False, description='状态')

    async def to_json(self, level=0, field_list: List = None):
        _fields_list = field_list
        if field_list is None:
            _fields_list = self._meta.fields
        d = {}
        for field in _fields_list:
            d[field] = getattr(self, field)
        # d["children"] = []
        return d

    class Meta:
        table = 'dept'
        ordering = ['-orderNo', 'id']
        table_description = '部门表'

    class PydanticMeta:
        exclude = ["created_at", "modified_at", "id", "is_delete"]


class User(TimestampMixin, AbstractBaseModel):
    username = fields.CharField(max_length=64, unique=True, description='用户名')
    nickname = fields.CharField(max_length=32, null=True, description='昵称')
    realName = fields.CharField(max_length=32, null=True, default=None, description='真实姓名')
    is_super = fields.SmallIntField(default=0, description='是否超级用户')
    mobile = fields.CharField(max_length=15, null=True, description='联系电话')
    email = fields.CharField(max_length=64, unique=True, description='email')
    role = fields.ForeignKeyField('models.Role', related_name='roles', to_field='value', description='角色')
    dept = fields.ForeignKeyField('models.Dept', related_name='dept', description='部门')
    password = fields.CharField(max_length=128, null=False, description='密码')
    avatar = fields.CharField(max_length=256, null=True, default=None, description='头像')
    address = fields.CharField(max_length=128, null=True, default=None, description='所在地区')
    remark = fields.CharField(max_length=256, null=True, default=None, description='备注')

    class Meta:
        table = "user"
        table_description = "用户表信息"
        ordering = ["-created_at", "id"]

    class PydanticMeta:
        exclude = ["modified_at", 'is_delete', 'password', 'is_super']


UserBase = pydantic_model_creator(User, name="UserBase", exclude_readonly=True)

# UserOut = pydantic_model_creator(
#     User,
#     name='UserOut',
#     include=['username', 'nickname', 'mobile', 'email'])
#
RoleBase = pydantic_model_creator(Role, name='RoleCreate', exclude_readonly=True)


# RoleOut = pydantic_model_creator(Role, name='RoleOut')
class RoleOut(RoleBase):
    id: int
    # menus: Optional[List[int]]
    # permissions: Optional[List[int]]


class RoleCreate(RoleBase):
    menu: Optional[List[int]]


DeptBase = pydantic_model_creator(Dept, name='DeptCreate')
DeptOut = pydantic_model_creator(Dept, name='DeptOut')


class DeptCreate(DeptBase):
    parent_id: Optional[int]
